<template>
  <div>
      <page-header :title="'就业资讯 ' + title" :content="'graduates ' + type" />
      <page-main>
          <el-row>
              <el-col :span="24">
                  <el-form ref='form' :model="form" label-width="120px">
                      <el-form-item label="标题" label-width="120px">   
                        <el-col :span="12">
                            <el-input v-model="form.title" :disabled="type == 'view'"/>
                        </el-col>
                      </el-form-item>
                      <el-form-item label="类型" label-width="120px">   
                        <el-col :span="6">
                            <el-select
                                v-model="form.type"
                                filterable
                                allow-create
                                default-first-option
                                :disabled="type == 'view'"
                                placeholder="请选择文章标签">
                                <el-option
                                v-for="item in options"
                                :key="item.value+item.label"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                      </el-form-item>
                      <el-form-item label="时间" label-width="120px">
                          <el-date-picker
                            v-model="form.issuedate"
                            type="datetime"
                            placeholder="选择日期时间"
                            align="right"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            :picker-options="pickerOptions">
                          </el-date-picker>
                      </el-form-item>
                      <el-form-item>
                        <editor v-model="form.content" />
                      </el-form-item>
                      <el-form-item label="">
                        <el-button type="primary" v-if="type != 'view'" @click="update()">确定</el-button>
                        <el-button @click="cannelBuild()">取消</el-button>
                      </el-form-item>
                  </el-form>
              </el-col>
          </el-row>
      </page-main>
  </div>
</template>

<script>
export default {
    name: 'graduates_update',
    data() {
        return {
            type: '',
            title: '',
            id: '',
            form: {},
            options: [{
                value: '公告',
                label: '公告'
            },{
                value: '资讯',
                label: '资讯'
            }],
            pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                    picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24);
                    picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', date);
                    }
                }]
            },
        }
    },
    watch:{
        '$route.path': function(newVal,oldVal){
            //console.log(newVal+"---"+oldVal);
            console.log(newVal);
        }
    },
    beforeRouteEnter(to, from, next) {
        let that = this
        next(vm => {
            // 通过 `vm` 访问fetchData，将query参数name，传递过去，进行逻辑处理
            // that.type = vm.$route.params.type
            vm.fetchData(vm.$route.params.type, vm.$route.params.id);
        });
    },
    methods: {
        fetchData(type, id) {
            this.type = type
            let titleList = {
                add: '新增',
                edit: '编辑',
                view: '查看'
            }
            this.id = id
            this.title = titleList[type]
            if (id != '*') {
                this.$util.post('public/selectNewsByPrimaryKey', {id: id}).then(res => {
                    this.form = res
                })
            }
        },
        cannelBuild() {
            this.form = {}
            this.$router.back(-1)
        },
        update() {
            if (this.type == 'add') {
                this.$util.post('public/insertNews', this.form).then(res => {
                    this.$message({
                        message: '新增成功',
                        type: 'success'
                    });
                    this.$router.push('/employment_manager/newsList')
                })
            } else {
                this.$util.post('public/updateNewsByPrimaryKey', this.form).then(res => {
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                    this.$router.push('/employment_manager/newsList')
                })
            }
        }
    }
}
</script>
